package Panel;

import Bean.Join;
import Dao.JoinDao;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class JoinDepotPanel extends JPanel {
    private JTable table;
    private JScrollPane jsp;

    public JoinDepotPanel() {
        this.setLayout(null);
        this.setBounds(0, 100, 680, 365);
        this.setBackground(new Color(66, 203, 222));
        init();
        this.setVisible(true);
    }

    public void init() {
        /**
         * top控件
         */
        JLabel oTil = new JLabel("订单号:");
        JComboBox oCb = new JComboBox(getOidList());
        JLabel joinTil = new JLabel("入库时间:");
        JTextField joinInp = new JTextField();
        JButton q = new JButton("搜索");
        oTil.setBounds(90, 0, 100, 30);
        oCb.setBounds(150, 5, 150, 20);
        joinTil.setBounds(350, 0, 100, 30);
        joinInp.setBounds(400, 5, 150, 20);
        q.setBounds(580, 5, 80, 20);
        this.add(q);
        this.add(joinInp);
        this.add(joinTil);
        this.add(oCb);
        this.add(oTil);
        /**
         * 表格控件
         */
        table = new JTable(getTableList(), new String[]{"编号", "订单号", "仓库编号", "货品名称", "入库时间", "重量", "备注"});
        table.setBounds(0, 0, 600, 200);
        jsp = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jsp.setBounds(40, 80, 600, 200);
        this.add(jsp);

        /**
         * bottom 按钮控件
         */
        JButton add = new JButton("添加");
        JButton mod = new JButton("修改");
        JButton del = new JButton("删除");
        add.setBounds(150,330,80,20);
        mod.setBounds(300,330,80,20);
        del.setBounds(450,330,80,20);
        this.add(add);
        this.add(mod);
        this.add(del);

        /**
         * 响应事件
         */
        q.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String item = (String) oCb.getSelectedItem();
                String time = joinInp.getText();
                List<Join> joinInfo = JoinDao.getJoinInfo(2, new String[]{item, time});
                reDraw(joinInfo);

            }
        });

        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                showModel("添加入库信息","添加");

            }
        });
        mod.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                showModel("修改入库信息","修改");
            }
        });
        del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                showModel("删除入库信息","删除");
            }
        });
    }

    /**
     *  获取入库数据
     * @return
     */
    public String[] getOidList() {
        List<Join> list = JoinDao.getJoinInfo(1,new String[]{});
        String[] oidStr = new String[list.size()];
        for (int i = 0; i < oidStr.length; i++) {
            oidStr[i] = String.valueOf(list.get(i).getOid());

        }
        return oidStr;
    }


    public String[][] getTableList() {
        List<Join> list = JoinDao.getJoinInfo(1,new String[]{""});
        String[][] info = new String[list.size()][7];
        for (int i = 0; i < list.size(); i++) {
            info[i][0] = String.valueOf(list.get(i).getId());
            info[i][1] = String.valueOf(list.get(i).getOid());
            info[i][2] = String.valueOf(list.get(i).getDid());
            info[i][3] = String.valueOf(list.get(i).getName());
            info[i][4] = String.valueOf(list.get(i).getJoinTime());
            info[i][5] = String.valueOf(list.get(i).getWeight());
            info[i][6] = String.valueOf(list.get(i).getRemark());
        }
        return info;
    }
    /**
     * 重画方法
     * 传入表格数据 -->二维数组
     */
    public void reDraw(List<Join> list){
        String[][] info = new String[list.size()][7];
        for (int i = 0; i < list.size(); i++) {
            info[i][0] = String.valueOf(list.get(i).getId());
            info[i][1] = String.valueOf(list.get(i).getOid());
            info[i][2] = String.valueOf(list.get(i).getDid());
            info[i][3] = String.valueOf(list.get(i).getName());
            info[i][4] = String.valueOf(list.get(i).getJoinTime());
            info[i][5] = String.valueOf(list.get(i).getWeight());
            info[i][6] = String.valueOf(list.get(i).getRemark());
        }
        this.setVisible(false);
        this.remove(jsp);
        table = new JTable(info,new String[]{"编号", "订单号", "仓库编号", "货品名称", "入库时间", "重量", "备注"});
        jsp.setViewportView(table);
        this.add(jsp);
        this.setVisible(true);
    }

    /**
     * 添加,修改,删除模态框
     */
    public JFrame showModel(String title,String buttonName){
        JFrame j = new JFrame(title);
        j.setLayout(null);
        j.setBounds(450,300,600,300);
        JLabel id = new JLabel("编号");
        JLabel oid = new JLabel("订单号");
        JLabel did = new JLabel("仓库编号");
        JLabel name = new JLabel("货品名称");
        JLabel time = new JLabel("入库时间");
        JLabel wight = new JLabel("重量");
        JLabel remark = new JLabel("备注");
        id.setBounds(0,0,100,20);
        oid.setBounds(200,0,100,20);
        did.setBounds(400,0,100,20);
        name.setBounds(0,100,100,20);
        time.setBounds(200,100,100,20);
        wight.setBounds(400,100,100,20);
        remark.setBounds(0,200,100,20);
        j.add(id);
        if(!"删除".equals(buttonName)){
            j.add(oid);
            j.add(did);
            j.add(name);
            j.add(time);
            j.add(wight);
            j.add(remark);
        }


        //输入框
        JTextField idI = new JTextField();
        JTextField oidI = new JTextField();
        JTextField didI = new JTextField();
        JTextField nameI = new JTextField();
        JTextField timeI = new JTextField();
        JTextField weightI = new JTextField();
        JTextField remarkI = new JTextField();
        idI.setBounds(50,0,120,20);
        oidI.setBounds(250,0,120,20);
        didI.setBounds(450,0,120,20);
        nameI.setBounds(50,100,120,20);
        timeI.setBounds(250,100,120,20);
        weightI.setBounds(450,100,120,20);
        remarkI.setBounds(50,200,120,20);
        j.add(idI);
        if(!"删除".equals(buttonName)){
            j.add(oidI);
            j.add(didI);
            j.add(nameI);
            j.add(timeI);
            j.add(weightI);
            j.add(remarkI);

        }

        //按钮
        JButton modelAdd = new JButton(buttonName);
        JButton modelColse = new JButton("关闭");
        modelAdd.setBounds(200,220,80,20);
        modelColse.setBounds(400,220,80,20);
        j.add(modelAdd);
        j.add(modelColse);

        //模态框按钮事件
        modelAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Join join = new Join();
                Boolean isSucc = null;
                try {
                    //判断id是否重复
                    List<Integer> id1 = JoinDao.findId();
                    if(!id1.contains(Integer.parseInt(idI.getText())) && "添加".equals(buttonName)){
                        join.setId(Integer.parseInt(idI.getText()));
                    }else if(id1.contains(Integer.parseInt(idI.getText())) && "添加".equals(buttonName)){
                        JOptionPane.showMessageDialog(j,"请不要输入相同的编号");
                        return;
                    }else  if(id1.contains(Integer.parseInt(idI.getText())) && "修改".equals(buttonName)){
                        join.setId(Integer.parseInt(idI.getText()));
                    }else if(!id1.contains(Integer.parseInt(idI.getText())) && "修改".equals(buttonName)){
                        JOptionPane.showMessageDialog(j,"请输入已经存在的编号");
                        return;
                    }else if(id1.contains(Integer.parseInt(idI.getText())) && "删除".equals(buttonName)){
                        join.setId(Integer.parseInt(idI.getText()));
                    }else {
                        JOptionPane.showMessageDialog(j,"请输入已经存在的编号");
                        return;
                    }
                    if(!"删除".equals(buttonName)){
                        join.setOid(Integer.parseInt(oidI.getText()));
                        join.setDid(Integer.parseInt(didI.getText()));
                        join.setName(nameI.getText());
                        join.setJoinTime(timeI.getText());
                        join.setWeight(Float.parseFloat(weightI.getText()));
                        join.setRemark(remarkI.getText());join.setOid(Integer.parseInt(oidI.getText()));
                        join.setDid(Integer.parseInt(didI.getText()));
                        join.setName(nameI.getText());
                        join.setJoinTime(timeI.getText());
                        join.setWeight(Float.parseFloat(weightI.getText()));
                        join.setRemark(remarkI.getText());
                    }
                }catch (NumberFormatException e){
                    JOptionPane.showMessageDialog(j,"输入类型错误 请重试");
                    return;
                }
                if("添加".equals(buttonName)){
                    isSucc = JoinDao.addJoinInfo(join);
                }else if("修改".equals(buttonName)){
                    isSucc = JoinDao.updateJoinInfo(join);
                }else if("删除".equals(buttonName)){
                    isSucc = JoinDao.deleteJoinInfo(join);
                }
                if(isSucc!= null && isSucc){
                    JOptionPane.showMessageDialog(j,buttonName+"成功");
                    reDraw(JoinDao.getJoinInfo(1,new String[]{}));
                }else {
                    JOptionPane.showMessageDialog(j,buttonName+"失败 请重试");
                }
            }
        });
        modelColse.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                j.dispose();
            }
        });
        j.setVisible(true);
        return j;
    }
}
